﻿using System;
//using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Google.GData.Client;

namespace Synchro
{
    public partial class IU : Form
    {
        private Synchro _synchro;
        public IU(Synchro synchro)
        {
            _synchro = synchro;
            InitializeComponent();
        }

        private void botClear_Click(object sender, EventArgs e)
        {
            labelResult.Text = "";
            labelSynchro.Visible = false;
            progressBar.Value = 0;
            progressBar.Visible = false;
        }

        private void botUser_Click(object sender, EventArgs e)
        {
            progressBarUser.Value = 0;
            labelInfoConn.Text = "Verifying Network...";
            progressBarUser.Value = 5;
            Refresh();
            if (_synchro.isConnectedInternet())
            {
                labelInfoConn.Text = "Verifying credentials...";
                Refresh();
                string user = textUser.Text;
                string password = textPassword.Text;
                progressBarUser.Value = 30;

                _synchro.google.credentials.User = user;
                _synchro.google.credentials.Password = password;

                /* Código para obtener todos los calendarios
                * y ponerlos en el ComboBox de la siguiente
                * pestaña */
                try
                {
                    ArrayList calendars = _synchro.google.updateCalendars();
                    progressBarUser.Value = 70;
                    labelInfoConn.Text = "Getting calendars...";
                    Refresh();
                    System.Threading.Thread.Sleep(1000);
                    if (calendars != null)
                    {
                        comboCalendar.Items.Clear();
                        for (int i = 0; i < calendars.Count; i++)
                        {
                            comboCalendar.Items.Add(calendars[i]);
                        }
                        comboCalendar.SelectedIndex = 0;
                        progressBarUser.Value = 100;
                        labelInfoConn.Text = "Credentials have been succesfully veryfied";
                    }
                    else
                    {
                        progressBarUser.Value = 100;
                        labelInfoConn.Text = "No Calendars available";
                    }
                }
                catch (AuthenticationException exc)
                {
                    progressBarUser.Value = 100;
                    labelInfoConn.Text = "Credentials are not correct";
                }
            }
            else {
                progressBarUser.Value = 100;
                labelInfoConn.Text = "Not Network Detected\nPlease, connect to a network and then click on Accept ...";
            }
        }

        private void botSynchro_Click(object sender, EventArgs e)
        {
            labelResult.Text = "";
            progressBar.Value = 0;
            progressBar.Visible = true;
            labelSynchro.Visible = true;
            Refresh();
            try
            {
                _synchro.google.modifyUri(comboCalendar.SelectedIndex);
                int result = _synchro.synchronize(comboOptions.SelectedIndex, comboCalendar.SelectedIndex, progressBar);
                labelSynchro.Visible = false;
                switch (result)
                {
                    case -1:
                        //labelResult.Text = "\nAppointments haven't been synchronized\n";
                        labelResult.Text = "An error has ocurred while synchronizing with Google Calendar \n";
                        break;
                    case -2:
                        //labelResult.Text = "\nAppointments haven't been synchronized\n";
                        labelResult.Text = "An error has ocurred while synchronizing with Outlook \n";
                        break;
                    case -500:
                        labelResult.Text = "You have not been logged yet\n";
                        break;
                    default:
                        labelResult.Text = "\n"+result+" Appointments have been synchronized\n";
                        break;
                }
                Refresh();
            }catch(System.ArgumentOutOfRangeException exc){
                progressBar.Value = 100;
                labelSynchro.Visible = false;
                labelResult.Text = "No Calendars available";
            }
        }
    }
}